package sizeyunsuan2;

import java.util.*;

public class Fuhao {

    private Stack<String> stack1;

    private List<String> list1,list2;

    private String message,Message="";

    public Fuhao() {

        stack1 = new Stack<String>();

        list1 = new ArrayList<String>();

        list2 = new ArrayList<String>();

    }

    //转换为后缀表达式

    public void evaluate(String expr) {

        String token;

        StringTokenizer tokenizer = new StringTokenizer(expr);

        while (tokenizer.hasMoreTokens()) {

            token = tokenizer.nextToken();

            if (token.equals("("))

                stack1.push(token);

            else if (token.equals("+") || token.equals("-")) {

                while (!stack1.empty()){

                    if(stack1.peek().equals("(")){

                        break;

                    }else list1.add(stack1.pop());

                }

                stack1.push(token);

            }else if (token.equals("*") || token.equals("/")) {

                if(!stack1.empty()) {

                    if (stack1.peek().equals("*") || stack1.peek().equals("/")) {

                        list1.add(stack1.pop());

                        stack1.push(token);

                    } else stack1.push(token);

                }else stack1.push(token);

            }

            else if (token.equals(")")) {

                while (!stack1.peek().equals("(")) {

                    list1.add(stack1.pop());

                }

                stack1.pop();

            }else list1.add(token);

        }

        while (!stack1.empty()) {

            list1.add(stack1.pop());

        }

        ListIterator<String > li = list1.listIterator();

        while (li.hasNext()) {

            Message += li.next() + " ";

            li.remove();

        }

        message = Message;

    }

    //转换

    public String getMessage(){

        return message;

    }

}


